/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.header.ServerHeader;
import java.text.ParseException;
import java.util.List;
import java.util.ListIterator;

/**
 * The Server header field contains information about the software used by
 * the UAS to handle the request. Revealing the specific software version of
 * the server might allow the server to become more vulnerable to attacks
 * against software that is known to contain security holes. Implementers
 * SHOULD make the Server header field a configurable option. If the Response
 * is being forwarded through a proxy, the proxy application must not modify
 * the ServerHeaders. Instead, it should include a ViaHeader.
 * <p/>
 * For Example:<br>
 * <code>Server: HomeServer v2</code>
 *
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 * @see javax.sip.header.ViaHeader
 * @see javax.sip.header.UserAgentHeader
 */
public class ServerHeaderImpl extends HeaderBase implements ServerHeader {
    private final static ServerHeaderParser parser = new ServerHeaderParser();

    private List<String> product;

    public ServerHeaderImpl() {
        super(ServerHeader.NAME);
    }

    ServerHeaderImpl(byte[] data, int start, int end) {
        super(ServerHeader.NAME, data, start, end);
    }

    public Object clone() {
        final ServerHeaderImpl header = new ServerHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.product = this.product;
        }
        return header;
    }

    protected HeaderParser getParser() {
        return parser;
    }

    protected String getValueAsString() {
        final StringBuilder sb = new StringBuilder();
        if (product != null && !product.isEmpty()) {
            String delimiter = "";
            for (String val : product) {
                sb.append(delimiter).append(val);
                delimiter = " ";
            }
        }
        return sb.toString();
    }

    /**
     * Returns a ListIterator over the List of product values.
     *
     * @return a ListIterator over the List of strings identifying the
     *         software of this ServerHeader
     */
    public ListIterator getProduct() {
        parse();
        return this.product.listIterator();
    }

    /**
     * Sets the List of product values of the ServerHeader.
     *
     * @param product a List of Strings specifying the product values
     * @throws java.text.ParseException which signals that an error has been reached
     *                                  unexpectedly while parsing the List of product value strings.
     */
    public void setProduct(List product) throws ParseException {
        parse();
        this.product = product;
        invalidateHeaderData();
    }
}
/*
 * $Log$
 */